import 'package:flutter/material.dart';
import 'dart:math' as math;

class CircularProgress extends StatelessWidget {
  /// 当前进度值，取值范围 0.0 - 1.0
  final double value;

  /// 进度条颜色
  final Color color;

  /// 背景颜色
  final Color backgroundColor;

  /// 进度条宽度
  final double? strokeWidth;

  /// 进度条半径大小
  final double radius;

  /// 是否显示中心文本
  final bool showText;

  /// 文本样式
  final TextStyle? textStyle;

  /// 是否显示边框
  final bool showBorder;

  /// 边框颜色
  final Color borderColor;

  const CircularProgress({
    super.key,
    this.value = 0.0,
    this.color = Colors.white70,
    this.backgroundColor = Colors.transparent,
    this.strokeWidth,
    this.radius = 20.0,
    this.showText = false,
    this.textStyle,
    this.showBorder = true,
    this.borderColor = Colors.white70,
  }) : assert(
            value >= 0.0 && value <= 1.0, 'Value must be between 0.0 and 1.0');

  @override
  Widget build(BuildContext context) {
    // 使用radius作为半径和笔触宽度，这样就能完全填充整个区域
    final double calculatedStrokeWidth = strokeWidth ?? radius;
    final String percentText = '${(value * 100).toInt()}%';

    return SizedBox(
      width: radius * 2,
      height: radius * 2,
      child: CustomPaint(
        painter: CircularProgressPainter(
          value: value,
          color: color,
          backgroundColor: backgroundColor,
          strokeWidth: calculatedStrokeWidth,
          showBorder: showBorder,
          borderColor: borderColor,
        ),
        child: showText
            ? Center(
                child: Text(
                  percentText,
                  style: textStyle ??
                      TextStyle(
                        fontSize: radius / 2.5,
                        fontWeight: FontWeight.bold,
                        color: color,
                      ),
                ),
              )
            : null,
      ),
    );
  }
}

/// 自定义环形进度条画笔
class CircularProgressPainter extends CustomPainter {
  final double value;
  final Color color;
  final Color backgroundColor;
  final double strokeWidth;
  final bool showBorder;
  final Color borderColor;

  CircularProgressPainter({
    required this.value,
    required this.color,
    required this.backgroundColor,
    required this.strokeWidth,
    this.showBorder = false,
    this.borderColor = Colors.white,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final Offset center = Offset(size.width / 2, size.height / 2);
    final double radius = math.min(size.width, size.height) / 2;

    if (showBorder) {
      // 绘制2像素宽的白色边框圆环
      final borderPaint = Paint()
        ..color = borderColor
        ..strokeWidth = 1.0 // 固定2像素宽度
        ..style = PaintingStyle.stroke;
      canvas.drawCircle(center, radius + 2, borderPaint);
    }

    if (value > 0) {
      final progressPaint = Paint()
        ..color = color
        ..strokeWidth = strokeWidth
        ..strokeCap = StrokeCap.butt
        ..style = PaintingStyle.stroke;
      final double safeStrokeWidth = math.min(strokeWidth, radius);
      canvas.drawArc(
        Rect.fromCircle(center: center, radius: radius - (safeStrokeWidth / 2)),
        -math.pi / 2, // 从顶部开始
        2 * math.pi * value, // 圆弧角度
        false,
        progressPaint,
      );
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}
